home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: simple code, argc, argv, strcmp()
- Date: 9 Feb 1996 05:49:48 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4fen9s$jjg@news.iag.net>
- References: <11f7cc$17261a.3b3@daprez> <4etj7c$bma@news.iag.net> <fcusack-0202961621470001@mudskipper.cac.psu.edu> <4f0rjn$sfh@news.iag.net> <823615465snz@genesis.demon.co.uk>
- NNTP-Posting-Host: pm2-orl23.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <823615465snz@genesis.demon.co.uk>, fred@genesis.demon.co.uk
- says...
- >
- >In article <4f0rjn$sfh@news.iag.net> jatmon@iag.net "John R Buchan" writes:
- >
- >>You are correct, of course. Stupidity on my part. I tend to get careless
- >>about strcmp's reverse logic (ie returns False, when the strings match),
- when
- >>I am not paying attention (not an excuse, just an explanation). I normally
- >>compare the return value to 0, just to keep myself straight.
- >
- >
- >That's the point though - strcmp() simply doesn't return a boolean value so
- >saying 'ie returns False' is just plain wrong and is what gets you into
- >trouble. strcmp() is in some sense more like a difference operation. e.g.
- >(ignoring overflow) if you take 2 integers a and b and calculate a-b the
- >result is >0 if a is larger, ==0 if they are equal and <0 if a is smaller,
- >just as strcmp(sa,sb) is defined. IMHO the definition of strcmp is quite
- >natural (but maybe the name doesn't quite reflect that definition).
-
- <sigh> Yes, I am well aware that strcmp doesn't return a boolean value. When
- it is actually used to determine the relative value of strings (ie in a sort
- function), it makes perfect sense and its use is quite straightforward.
-
- However, when you use it as the condition of an if (or any similar
- conditional) the only thing that matters is whether the return is zero or
- non-zero (False or True). I guess it's a weakness of mine, but I tend to
- read conditions (or any essentially boolean expressions) in terms of True and
- False (an unfortunate side effect of this is that I can make the mistake
- mentioned above). When the function's name doesn't make its 'boolean' value
- obvious, I find the code much easier to read, if I compare the return to
- some 'obvious' value (eg, 0, NULL, etc).
-
- As you indicated the name 'strcmp' doesn't really read well in a boolean.
- Maybe it should changed to something like:
-
- if( theseStringsAreDifferent(str1, str2) ) /* Is this a bit wordy? :-} */
-
- ....but that name doesn't make sense in a relative value statement. Hmm..:)
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-